Skip to content

[test-improver] Improve tests for version package#4140

Merged
lpcox merged 1 commit intomainfrom
test-improver/version-build-version-string-2daeb533f6ad4540
Apr 19, 2026
Merged

[test-improver] Improve tests for version package#4140
lpcox merged 1 commit intomainfrom
test-improver/version-build-version-string-2daeb533f6ad4540

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Test Improvements: version_test.go

File Analyzed

  • Test File: internal/version/version_test.go
  • Package: internal/version
  • Lines of Code: 85 → 144 (+59 lines)

Improvements Made

1. Increased Coverage

  • ✅ Added TestBuildVersionString with 8 subtests covering all explicit-parameter branches of BuildVersionString
  • Previous Coverage: 12% (only Set and Get were tested; BuildVersionString had 0%)
  • New Coverage: 72%
  • Improvement: +60%

2. Better Testing Patterns

  • ✅ Used t.Run subtests for each logical scenario in TestBuildVersionString
  • ✅ Used assert.Equal for deterministic cases (all three parameters supplied)
  • ✅ Used strings.HasPrefix / strings.HasSuffix / assert.Contains for partially-deterministic cases where the debug.ReadBuildInfo() fallback may or may not append extra fields
  • ✅ Added require.GreaterOrEqual to guard against panics when splitting output into parts

3. Branches Covered

Branch Covered? Notes
mainVersion != "" → use mainVersion Deterministic
mainVersion == "" → use "dev" Deterministic
gitCommit != "" → use "commit: <gitCommit>" Deterministic
gitCommit == "" → fall back to debug.ReadBuildInfo() vcs.revision ✅ (entry) Inner body not coverable in unit tests (no embedded VCS metadata)
buildDate != "" → use "built: <buildDate>" Deterministic
buildDate == "" → fall back to debug.ReadBuildInfo() vcs.time ✅ (entry) Inner body not coverable in unit tests
explicit gitCommit is not truncated (truncation only in fallback) Documents the truncation behaviour contract

The remaining uncovered lines (~32% of BuildVersionString) are the loop bodies that match vcs.revision/vcs.time settings inside debug.ReadBuildInfo(); these are only populated when the binary is built with embedded VCS metadata via go build -ldflags and cannot be exercised in unit tests.

Test Execution

All tests pass:

=== RUN   TestBuildVersionString
=== RUN   TestBuildVersionString/all_fields_provided
=== RUN   TestBuildVersionString/empty_mainVersion_uses_dev_prefix
=== RUN   TestBuildVersionString/mainVersion_and_gitCommit_without_buildDate
=== RUN   TestBuildVersionString/mainVersion_and_buildDate_without_gitCommit
=== RUN   TestBuildVersionString/only_mainVersion
=== RUN   TestBuildVersionString/all_empty_uses_dev
=== RUN   TestBuildVersionString/result_is_comma-separated
=== RUN   TestBuildVersionString/explicit_gitCommit_is_not_truncated
--- PASS: TestBuildVersionString (0.00s)
PASS
coverage: 72.0% of statements
ok  	github.com/github/gh-aw-mcpg/internal/version	0.004s

Why These Changes?

BuildVersionString constructs the version string displayed in health checks, --version output, and MCP client metadata. It had zero test coverage despite having six distinct branches. The new tests document the function's contract for all explicitly-supplied-parameter paths and confirm stability across builds where debug.ReadBuildInfo() may or may not provide VCS metadata.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver · ● 972.4K ·

Add TestBuildVersionString with 8 subtests covering all explicit-parameter
branches of BuildVersionString. Coverage of the function rises from 0% to
68%, and overall package coverage rises from 12% to 72%.

The remaining uncovered lines are the debug.ReadBuildInfo() fallback paths
(vcs.revision and vcs.time settings) which are only populated when the binary
is built with embedded VCS metadata and cannot be exercised in unit tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 19, 2026 21:47
Copilot AI review requested due to automatic review settings April 19, 2026 21:47
@lpcox lpcox merged commit 0ae43b4 into main Apr 19, 2026
14 checks passed
@lpcox lpcox deleted the test-improver/version-build-version-string-2daeb533f6ad4540 branch April 19, 2026 21:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves unit test coverage for internal/version by adding comprehensive tests for BuildVersionString, documenting and validating its output across key parameter combinations (including the debug.ReadBuildInfo() fallback behavior).

Changes:

  • Added TestBuildVersionString with subtests covering the primary branching paths (explicit parameters and fallback-entry behavior).
  • Expanded assertions to be stable across environments where Go may or may not embed VCS metadata in debug.ReadBuildInfo().
Show a summary per file
File Description
internal/version/version_test.go Adds subtests and assertions to cover BuildVersionString branches and improve package test coverage.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants